package cn.duyo.spring.tx.transaction.interceptor;

import cn.duyo.spring.tx.transaction.support.DefaultTransactionDefinition;

/**
 * 这个类并没做特殊的事情，就是常规属性的设置
 * 对于`rollbackOn(Throwable ex)`这个方法在这里进行了实现。如果当前异常是运行时异常或者error，那么返回的是ture
 *
 * @author du
 */
public class DefaultTransactionAttribute extends DefaultTransactionDefinition implements TransactionAttribute {


    public DefaultTransactionAttribute() {
        super();
    }


    /**
     * 判断是否需要回滚：运行时异常 or Error需要回滚
     * Exception分为受检异常，和非受检异常，RuntimeException则是非受检异常，无需强制抛出
     * Throwable -> Exception(异常) -> IOException        -> EOFException
     *            |                                      -> FileNotFoundException
     *            |                                      -> MalformedURLException
     *            |                                      -> UnknownHostException
     *            |                -> ClassNotFoundException
     *            |                -> CloneNotSupportException
     *            |                -> RuntimeException   -> ArithmeticException
     *            |                                      -> ClassCastException
     *            |                                      -> IllegalStateException
     *            |_______________________________       -> IllegalArgumentException/IndexOutOfBoundsException/NoSuchElementException/NullPointerException
     *           -> Error (错误，JVM的内部错误，资源耗尽等)
     *
     *
     * @param ex
     * @return
     */
    @Override
    public boolean rollbackOn(Throwable ex) {
        return (ex instanceof RuntimeException || ex instanceof Error);
    }

    @Override
    public String toString() {
        return "DefaultTransactionAttribute{}";
    }
}
